package com.bw.zzy.partal2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.bw.zzy.partal2.common.CommonResult;
import com.bw.zzy.partal2.controller.form.*;
import com.bw.zzy.partal2.service.DoctorService;
import com.bw.zzy.partal2.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.xml.stream.events.Comment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/doctor")
@Tag(name = "DocterController", description = "医生管理接口")
@CrossOrigin(origins = "http://localhost:4000",allowCredentials = "true")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    /*
     *
     * */

    @PostMapping("/selectConditionByPage")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:SELECT"}, mode = SaMode.OR)//有其中一个permission就可以执行
    @Operation(summary = "分页查询")
    public CommonResult selectConditionByPage(@RequestBody @Valid SelectDoctorByPageForm form) {  //@Valid 让 SelectDockerByForm 里面的注解都生效

        Map param = BeanUtil.beanToMap(form);  //转换成map

        int page = form.getPage();          //前端传递过来
        int length = form.getLength();      //

        //计算起始页
        int start = (page - 1) * length;

        param.put("start", start);  //传参给 sql limit查询
        PageUtils pageUtils = doctorService.selectConditionByPage(param);
        return CommonResult.ok().put("result", pageUtils);
    }


    @PostMapping("/selectDoctorDetailById")
    @SaCheckLogin
    @Operation(summary = "通过id查询医生详细信息")
    @SaCheckPermission(value = {"ROOT","DOCTOR:SELECT"},mode = SaMode.OR)
    public CommonResult selectDoctorDetailById(@RequestBody @Valid SelectDoctorDetailByIdForm form) {
        HashMap hashMap = doctorService.selectDoctorDetailById(form.getId());  //前端传入id，这里拿到
        return CommonResult.ok().put( "doctor",hashMap);   //
    }


    @PostMapping("/insert")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:INSERT"}, mode = SaMode.OR)
    @Operation(summary = "添加医生信息")
    public CommonResult insert(@RequestBody @Valid InsertDoctorForm form) {
        Map param = BeanUtil.beanToMap(form);   //将bean转换成一个Map集合，

        //将String[]先toString--》字符串 ，在转换成JSON格式
        String json = JSONUtil.parseArray(form.getTag()).toString();
        param.replace("tag", json);

        param.put("uuid", IdUtil.simpleUUID().toUpperCase());//生成uuid，放入param中
        doctorService.insert(param);
        return CommonResult.ok();
    }


    @PostMapping("/updatePhoto")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","DOCTOR:UPDATE"},mode = SaMode.OR)
    @Operation(summary = "照骗上传")
    public CommonResult updatePhoto(@Param("file")MultipartFile file , @Param("doctorId")Integer doctorId) {
        doctorService.updatepicture(file, doctorId);
        return CommonResult.ok();
    }


    @PostMapping("/selectById")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","DOCTOR:SELECT"},mode = SaMode.OR)
    @Operation(summary = "通过查询医生信息，修改数据回显")
    public CommonResult searchById(@RequestBody @Valid SelectDoctorByIdForm form) {
        HashMap map = doctorService.selectById(form.getId());
        return CommonResult.ok(map);
    }


    @PostMapping("/update")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:SELECT"}, mode = SaMode.OR)
    @Operation(summary = "修改医生信息")
    public CommonResult update(@RequestBody @Valid UpdateDoctorForm form) {
        Map param = BeanUtil.beanToMap(form);
        String json = JSONUtil.parseArray(form.getTag()).toString();
        param.replace("tag", json);
        doctorService.update(param);
        return CommonResult.ok();
    }


    @PostMapping("/deleteDoctorByIds")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:DELETE"}, mode = SaMode.OR)
    @Operation(summary = "删除医生信息")
    public CommonResult deleteByIds(@RequestBody @Valid DeleteDoctorByIdsForm form) {
        doctorService.deleteDoctorByIds(form.getIds());
        return CommonResult.ok();

    }

    @Operation(summary = "获取关联诊室的所有医生")
    @PostMapping("/selectDoctorsBySubId")
    @SaCheckLogin
    public CommonResult selectDpctorBySubId(@RequestBody @Valid SelectDoctorBySubIdForm form) {
        ArrayList<HashMap> result = doctorService.selectDoctorBySubId(form.getDeptSubId());
        return CommonResult.ok().put("result", result);
    }
}